<template>
  <div>
    <div class="dashboard-container">
      <div class="app-container">
        <el-card class="tree-card" v-loading="loading">
          <!-- 用了一个行列布局 -->
          <tree-tools :treeNode="company" :isRoot="true" @addDepts="addDepts" />
          <el-tree
            :data="departs"
            :props="defaultProps"
            :default-expand-all="true"
          >
            <tree-tools
              slot-scope="{ data }"
              :treeNode="data"
              @delDepts="getDepartments"
              @addDepts="addDepts"
              @editDepts="editDepts"
            />
          </el-tree>
        </el-card>
        <!-- 下面的菜单 -->
      </div>
    </div>
    <!-- 弹出框 -->
    <add-dept
      :show-dialog="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
      :showDialog.sync="showDialog"
      ref="addDept"
    />
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils/index";
import AddDept from "./components/add-dept";
export default {
  components: { TreeTools, AddDept },
  data() {
    return {
      company: {},
      loading: false,
      defaultProps: {
        label: "name"
      },
      departs: [],
      showDialog: false,
      node: null
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      this.loading = true;
      const result = await getDepartments();
      this.company = { name: "小懒虫中心系统", manager: "负责人", id: "" };
      this.departs = tranListToTreeData(result.depts, "");
      this.loading = false;
    },
    addDepts(node) {
      this.showDialog = true;
      this.node = node;
    },
    editDepts(node) {
      this.showDialog = true;
      this.node = node;
      this.$refs.addDept.getDepartDetail(node.id);
    }
  }
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
